package com.cjb.cloudsystem.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjb.cloudsystem.entity.SysUserToken;
import com.cjb.cloudsystem.jwt.TokenUtil;
import com.cjb.cloudsystem.mapper.SysUserTokenMapper;
import com.cjb.cloudsystem.service.SysUserTokenService;
import com.cjb.common.exception.ServerException;
import com.cjb.common.mybatisUtil.impl.BaseRepositoryImpl;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author chenjinbin
 * @Package IntelliJ IDEA
 * @Class SysUserTokensServiceImpl
 * @Date 2023-07-27 21:41
 */
@Service
@Slf4j
public class SysUserTokenRepositoryImpl extends BaseRepositoryImpl<SysUserTokenMapper, SysUserToken> implements SysUserTokenService {
    /**
     * 注入token工具类
     */
    @Autowired
    private TokenUtil tokenUtil;
    /*
     * 注入mapper层
     */
    @Autowired
    private SysUserTokenMapper sysUserTokenMapper;

    /**
     * 单体保存
     *
     * @return
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @Override
    public boolean save(SysUserToken sysUserToken) {
        baseMapper.insert(sysUserToken);
        return true;
    }

    /**
     * 批量保存
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @Override
    public Integer listSave(List<SysUserToken> sysUserToken) {
        return sysUserTokenMapper.insertBatchSomeColumn(sysUserToken);
    }

    /**
     * 批量更新
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @Override
    public void listUpdate(List<SysUserToken> sysUserToken) {
        sysUserTokenMapper.insertBatchSomeColumn(sysUserToken);
    }

    /**
     * 批量删除
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @Override
    public void listDelete(List<SysUserToken> sysUserToken) {
        List<Long> collect = sysUserToken.stream().map(SysUserToken::getId)
                .distinct()
                .collect(Collectors.toList());
        sysUserTokenMapper.deleteBatchIds(collect);
    }

    /**
     * 通过id删除
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @Override
    public int deleteById(SysUserToken sysUserToken) {
        return sysUserTokenMapper.deleteById(sysUserToken.getId());
    }

    /**
     * 单个查询
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @Override
    public SysUserToken queryById(SysUserToken sysUserToken) {
        return sysUserTokenMapper.selectById(sysUserToken.getId());
    }

    @Override
    public Page queryPage(Page sysUserTokenPage, SysUserToken sysUserToken) {
        return sysUserTokenMapper.selectMapsPage(sysUserTokenPage, null);
    }

    /**
     * 返回全部信息
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @Override
    public List<SysUserToken> returnAllData() {
        return sysUserTokenMapper.selectList(null);
    }

    /**
     * 创建token
     *
     * @param
     * @return
     * @author chenjinbin
     * @email 2213859123@qq.com
     * @date 2023-07-12 9:10
     */
    @Override
    public SysUserToken createToken(SysUserToken sysUserToken) {
        //token生成
        String accessToken = tokenUtil.geneJsonWebToken(sysUserToken.getUserId().toString());
        if (StrUtil.isBlank(accessToken)){
            throw new ServerException("token生成错误");
        }
        sysUserToken.setAccessToken(accessToken);
        return sysUserToken;
    }

}